package com.touchstone.cloudpacs.controller;

import com.touchstone.cloudpacs.common.utils.request.PageRequest;
import com.touchstone.cloudpacs.common.utils.result.ResultVO;
import com.touchstone.cloudpacs.common.validate.Insert;
import com.touchstone.cloudpacs.common.validate.Update;
import com.touchstone.cloudpacs.request.UserRequest;
import com.touchstone.cloudpacs.request.query.UserPageQuery;
import com.touchstone.cloudpacs.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @Description : 用户控制层
 * @Author : hunter
 * @Date: 2019-07-11 20:25
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/user")
@Api(description = "用户", tags = "用户信息")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/save")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    public ResultVO save(@Validated(Insert.class) UserRequest userRequest) {
        return ResultVO.success(userService.save(userRequest));
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    public ResultVO update(@Validated(Update.class) UserRequest userRequest) {
        return ResultVO.success(userService.update(userRequest));
    }

    @PostMapping("/disabled")
    @ApiOperation(value = "禁用用户", notes = "禁用用户状态")
    public ResultVO disabled(@RequestParam("id") @Validated @NotBlank(message = "用户ID不能为空") String id) {
        return ResultVO.success(userService.disabled(id));
    }

    @PostMapping("/enable")
    @ApiOperation(value = "启用用户", notes = "启用用户用户状态")
    public ResultVO enable(@RequestParam("id") @Validated @NotBlank(message = "用户ID不能为空") String id) {
        return ResultVO.success(userService.enable(id));
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询一条用户信息", notes = "查询一条用户信息")
    public ResultVO get(@ApiParam(value = "用户ID", required = true) @RequestParam @Validated @NotBlank(message = "用户ID不能为空") String id) {
        return ResultVO.success(userService.get(id));
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询用户", notes = "分页查询用户数据")
    public ResultVO page(@RequestBody PageRequest<UserPageQuery> pageRequest) {
        return ResultVO.usePageInfo(userService.selectByPage(pageRequest));
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    public ResultVO login(@RequestParam @Validated @NotBlank(message = "用户名不能为空") String userName,
                          @RequestParam @Validated @NotBlank(message = "密码不能为空") String password) {
        userService.login(userName, password);
        return ResultVO.success();
    }

    @PostMapping("/logout")
    @ApiOperation(value = "注销", notes = "注销")
    public ResultVO logout() {
        userService.logout();
        return ResultVO.success();
    }
}
